package org.gcu.filestorage.controller;

import org.gcu.filestorage.api.ErrorCode;
import org.gcu.filestorage.pojo.AccountInfo;
import org.gcu.filestorage.pojo.RequestResult;
import org.gcu.filestorage.serive.AccountInfoService;
import org.gcu.filestorage.serive.MailService;
import org.gcu.filestorage.uitls.MD5Util;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
public class ResetPasswordController {

    private final AccountInfoService accountInfoService;

    private final MailService mailService;
    private final RedisController redisController;

    public ResetPasswordController(AccountInfoService accountInfoService,
                                   MailService mailService,
                                   RedisController redisController) {
        this.accountInfoService = accountInfoService;
        this.mailService = mailService;
        this.redisController = redisController;
    }

    @GetMapping("/validateEmail")
    public RequestResult<Boolean> validateEmail(@RequestParam String account, @RequestParam String email) {
        List<AccountInfo> accountInfoList = this.accountInfoService.queryAccountInfoByEmail(email);
        boolean isEmailExisted = (accountInfoList!= null
                && !accountInfoList.isEmpty()
                && accountInfoList.get(0).getAccount().equals(account));
        RequestResult<Boolean> requestResult = new RequestResult<>();
        requestResult.setData(isEmailExisted);
        if (isEmailExisted) {
            Random random = new Random();
            int randomNumber = random.nextInt(9000) + 1000;
            redisController.save(account,  randomNumber + "", 60); // TODO 将保留时间也下发到客户端, 做倒计时使用
            mailService.sendSimpleMail(email, "重置密码验证码",  randomNumber + "");
            requestResult.setMessage("验证码已发送至您邮箱，请查收");
            requestResult.setResCode(ErrorCode.SUCCESS);
        } else {
            requestResult.setMessage("输入的账号或邮箱有误");
            requestResult.setResCode(ErrorCode.VALIDATE_EMAIL_FAILURE);
        }
        return requestResult;
    }


    @GetMapping("/validationCode")
    public RequestResult<String> getValidationCode(@RequestParam String account) {
        return redisController.get(account);
    }


    @PostMapping("/resetPassword")
    public RequestResult<Boolean> resetPassword(@RequestParam String account, @RequestParam String resetPassword) {
        boolean b = accountInfoService.updatePassword(account, MD5Util.encryptByMD5(resetPassword));
        RequestResult<Boolean> requestResult = new RequestResult<>();
        requestResult.setResCode(b ? ErrorCode.SUCCESS: ErrorCode.RESET_PASSWORD_FAILURE);
        requestResult.setData(b);
        requestResult.setMessage(b ? "重置密码成功" : "重置密码失败");
        return requestResult;
    }

}
